{{#if targetPackage ~}}
    package {{targetPackage}};
{{/if}}
{{#each imports ~}}
    import {{this}};
{{/each}}
public class {{name}} {

    private final static String TABLE_NAME = "{{table.name}}";

    private final static String[] COLUMNS = new String[]{
    {{#each table.columns ~}}
       "{{this.field}}",
    {{~/each}}
    };

    public String save(final {{entityName}} {{upperCamel2LowerCamel entityName}}) {
        return new SQL(){ {
            INSERT_INTO(TABLE_NAME);
            {{#each table.columns ~}}
                if( {{upperCamel2LowerCamel entityName}}.get{{lowerUnderScore2UpperCamel this.field}}() != null) VALUES("{{this.field}}", "#{ {{~ upperCamel2LowerCamel entityName}}.{{lowerUnderScore2LowerCamel this.field ~}} }");
            {{~/each}}
         } }.toString();
    }

    public String update(final {{entityName}} {{upperCamel2LowerCamel entityName}}) {
        return new SQL(){ {
            UPDATE(TABLE_NAME);
            {{#each table.columns ~}}
                if( {{upperCamel2LowerCamel entityName}}.get{{lowerUnderScore2UpperCamel this.field}}() != null) SET("{{this.field}}", "#{ {{~ upperCamel2LowerCamel entityName}}.{{lowerUnderScore2LowerCamel this.field ~}} }");
            {{~/each}}
            WHERE("{{primaryKeyColumn.field}} = #{ {{~ upperCamel2LowerCamel entityName}}.{{primaryKeyColumn.field ~}} }");
         } }.toString();
    }


   public String deleteById(final {{primaryKeyColumn.javaType.type.simpleName}} {{lowerUnderScore2LowerCamel primaryKeyColumn.field}}) {
        return new SQL(){ {
            DELETE_FROM(TABLE_NAME);
            WHERE("{{primaryKeyColumn.field}} = #{ {{~ primaryKeyColumn.field ~}} }");
         } }.toString();
   }

   public String getById(final {{primaryKeyColumn.javaType.type.simpleName}} {{lowerUnderScore2LowerCamel primaryKeyColumn.field}}) {
       return new SQL(){ {
            SELECT(COLUMNS);
            FROM(TABLE_NAME);
            WHERE("{{primaryKeyColumn.field}} = #{ {{~ primaryKeyColumn.field ~}} }");
        } }.toString();
   }

    public String page(final {{entityName}} {{upperCamel2LowerCamel entityName}}) {
        return new SQL(){ {
            SELECT(COLUMNS);
            FROM(TABLE_NAME);
            {{#each table.columns ~}}
                if( {{upperCamel2LowerCamel entityName}}.get{{lowerUnderScore2UpperCamel this.field}}() != null) WHERE("{{this.field}} = #{ {{~ upperCamel2LowerCamel entityName}}.{{lowerUnderScore2LowerCamel this.field ~}} }");
            {{~/each}}
        } }.toString();
    }

}